package com.bootdo.member.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.mall_part.service.MallHousePlotService;
import com.bootdo.member.domain.ShequIntegralRecord;
import com.bootdo.member.service.ShequIntegralRecordService;
import com.bootdo.system.domain.UserDO;
import com.front.web.commons.AvoidRepeatableCommit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.member.domain.ShequMember;
import com.bootdo.member.service.ShequMemberService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 用户表
 * 
 * @author lujingqian
 * @email 2100272728@qq.com
 * @date 2019-11-08 14:04:00
 */
 
@Controller
@RequestMapping("/member/shequMember")
public class ShequMemberController extends BaseController{


	@Autowired
	private ShequMemberService shequMemberService;
	@Autowired
	private ShequIntegralRecordService shequIntegralRecordService;
	@Autowired
	private MallHousePlotService mallHousePlotService;

	@ModelAttribute
	public ShequMember get(@RequestParam(required=false) Long id) {
		if (id != null){
			return shequMemberService.get(id);
		}else{
			return new ShequMember();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/member/shequMember','member:shequMember:shequMember',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','member:shequMember:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("member:shequMember:shequMember")	
	String ShequMember(ShequMember shequMember,HttpServletRequest request,HttpServletResponse response){

		return "member/shequMember/shequMember";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("member:shequMember:shequMember")
	public PageUtils list(@RequestParam Map<String, Object> params){

		UserDO user = ShiroUtils.getUser();
		if(user.getUserType()!=1){
			params.put("communityId",user.getCommunityId());
		}
		//查询列表数据
		Query query = new Query(params);
		List<ShequMember> shequMemberList = shequMemberService.list(query);
		int total = shequMemberService.count(query);
		PageUtils pageUtils = new PageUtils(shequMemberList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("member:shequMember:edit")
	String add(ShequMember shequMember,HttpServletRequest request,HttpServletResponse response){
	    return "member/shequMember/shequMemberedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(ShequMember shequMember) {
		return R.ok().put("i",shequMember);
	}


    /**
     * 物业端 提交申请使用积分兑换物业费
     * @param id
     * @param number
     * @param remarks
     * @return
     */
	@PostMapping("/buyProperty")
	@ResponseBody
	@AvoidRepeatableCommit
	@Transactional
	R buyProperty(@RequestParam(value = "id",defaultValue = "")Long id,
				  @RequestParam(value = "number",defaultValue = "")Double number,
				  @RequestParam(value = "remarks",defaultValue = "")String remarks) {

        ShequMember shequMember = shequMemberService.get(id);
        if(number>shequMember.getIntegral()){
            return R.error("积分不足");
        }
		UserDO user = ShiroUtils.getUser();
		StringBuffer sb= new StringBuffer(remarks);
        sb.append("<br/>操作人："+user.getName());
        ShequIntegralRecord shequIntegralRecord = new ShequIntegralRecord();
        shequIntegralRecord.setIntegrals(number);
       /* shequIntegralRecord.setMemberId(id);*/
        shequIntegralRecord.setMemberId(id);
        shequIntegralRecord.setCountType(2);//1为 增加  0为消减  2为没有扣除或者增加
        shequIntegralRecord.setMemberName(shequMember.getRealName());
        shequIntegralRecord.setMemberMobile(shequMember.getMobile());
        shequIntegralRecord.setRemarks(sb.toString());
        shequIntegralRecord.setState(0);//未确认
        shequIntegralRecord.setOperationType(1);//该操作为物业管理员
        shequIntegralRecord.setCommunityId(shequMember.getCommunityId());
        shequIntegralRecord.setCommunityName(shequMember.getCommunityName());
        int save = shequIntegralRecordService.save(shequIntegralRecord);
        if(save>0){
           /*shequMemberService.updateIntegerMinus(number,shequMember.getMobile());*/
        }
        return R.ok();
	}

	/**
	 * 物业端 提交申请增加积分
	 * @param id
	 * @param number
	 * @param remarks
	 * @return
	 */
	@PostMapping("/addProperty")
	@ResponseBody
	@AvoidRepeatableCommit
	@Transactional
	R addProperty(@RequestParam(value = "id",defaultValue = "")Long id,
				  @RequestParam(value = "number",defaultValue = "")Double number,
				  @RequestParam(value = "remarks",defaultValue = "")String remarks) {

		UserDO user = ShiroUtils.getUser();
		StringBuffer sb= new StringBuffer(remarks);
		sb.append("<br/>操作人："+user.getName());
		ShequMember shequMember = shequMemberService.get(id);
		ShequIntegralRecord shequIntegralRecord = new ShequIntegralRecord();
		shequIntegralRecord.setIntegrals(number);
		shequIntegralRecord.setMemberId(id);
		shequIntegralRecord.setMemberName(shequMember.getRealName());
		shequIntegralRecord.setMemberMobile(shequMember.getMobile());
		shequIntegralRecord.setRemarks(sb.toString());
		shequIntegralRecord.setState(1);//已确认
		shequIntegralRecord.setCountType(1);//1为 增加  0为消减  2为没有扣除或者增加
		shequIntegralRecord.setOperationType(0);//该操作为其他人员
		shequIntegralRecord.setCommunityId(shequMember.getCommunityId());
		shequIntegralRecord.setCommunityName(shequMember.getCommunityName());
		int save = shequIntegralRecordService.save(shequIntegralRecord);
		if(save>0){
			shequMemberService.updateIntegerAdd(number,shequMember.getMobile());
		}
		return R.ok();
	}
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("member:shequMember:edit")
	public R save( ShequMember shequMember){
	
		if(shequMemberService.save(shequMember)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("member:shequMember:edit")
	public R remove( Long id){
		if(shequMemberService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("member:shequMember:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		shequMemberService.batchRemove(ids);
		return R.ok();
	}

}
